package com.hlhj.funlike_businessqzxd.utils;

import android.app.DialogFragment;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.hlhj.funlike_businessqzxd.R;
import com.hlhj.funlike_businessqzxd.activity.SettingsAty;

import static com.hlhj.funlike_businessqzxd.activity.SettingsAty.dataBeanList;

public class Dialog extends DialogFragment {
    private View view;
    private TextView btn_ok,tv_otherTake,tv_mangoTake,tv_merchantTake;
    private EditText ed_time1,ed_1;
    private RadioButton radio_btn1,radio_btn2,radio_btn3;
    private String takeType,freight;
    private int freight_long,id;   //配送费用 配送时长
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = inflater.inflate(R.layout.fgm_dialog,null);
        initView();
        initData();
        return view;
    }

    private void initView() {
        btn_ok=view.findViewById(R.id.btn_ok);
        tv_otherTake=view.findViewById(R.id.tv_otherTake);
        tv_mangoTake=view.findViewById(R.id.tv_mangoTake);
        tv_merchantTake=view.findViewById(R.id.tv_merchantTake);
        radio_btn1=view.findViewById(R.id.radio_btn1);
        radio_btn2=view.findViewById(R.id.radio_btn2);
        radio_btn3=view.findViewById(R.id.radio_btn3);
        ed_time1=view.findViewById(R.id.ed_time1);
        ed_1=view.findViewById(R.id.ed_1);

        tv_merchantTake.setText(dataBeanList.get(0).getName());
        tv_mangoTake.setText(dataBeanList.get(1).getName());
        tv_otherTake.setText(dataBeanList.get(2).getName());
        id = dataBeanList.get(0).getId();
        freight = "0";
        freight_long=0;
    }

    private void initData() {
        ed_time1.setText(SettingsAty.freight_long+"");
        ed_1.setText(SettingsAty.freight);

        radio_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radio_btn1.setChecked(true);
                radio_btn2.setChecked(false);
                radio_btn3.setChecked(false);
                takeType=tv_merchantTake.getText().toString();
                id=dataBeanList.get(0).getId();
                freight = ed_time1.getText().toString();
                freight_long = Integer.parseInt(ed_1.getText().toString());
                Log.e("zy",id + "-" + freight + "-" + freight_long);
            }
        });
        radio_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radio_btn1.setChecked(false);
                radio_btn2.setChecked(true);
                radio_btn3.setChecked(false);
                takeType=tv_mangoTake.getText().toString();
                id=dataBeanList.get(1).getId();
                freight="0";
                freight_long=0;
            }
        });
        radio_btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radio_btn1.setChecked(false);
                radio_btn2.setChecked(false);
                radio_btn3.setChecked(true);
                takeType=tv_otherTake.getText().toString();
                id=dataBeanList.get(2).getId();
                freight="0";
                freight_long=0;
            }
        });

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lisenter!=null){
                    lisenter.getType(takeType,id,freight,freight_long);
                }
            }
        });
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window!=null){
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
            getDialog().getWindow().setLayout( dm.widthPixels, getDialog().getWindow().getAttributes().height );
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.BOTTOM;
            window.setAttributes(params);
            window.setBackgroundDrawable( new ColorDrawable( 0x00000000 ) );
        }
    }

    public interface getTakeType{
        void getType(String type,int id,String freight,int freight_long);
    }

    public getTakeType lisenter;

    public void setLisenter(getTakeType lisenter) {
        this.lisenter = lisenter;
    }
}
